Option Strict On
Option Explicit On 

Public Class UUniqueConstraint
    Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lblConstraintName As System.Windows.Forms.Label
    Friend WithEvents txtConstraintName As System.Windows.Forms.TextBox
    Friend WithEvents lstColumns As System.Windows.Forms.CheckedListBox
    Friend WithEvents lblColumns As System.Windows.Forms.Label
    Friend WithEvents chkPrimaryKey As System.Windows.Forms.CheckBox
    Friend WithEvents ErrorProvider1 As System.Windows.Forms.ErrorProvider
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(UUniqueConstraint))
        Me.lblConstraintName = New System.Windows.Forms.Label
        Me.txtConstraintName = New System.Windows.Forms.TextBox
        Me.lstColumns = New System.Windows.Forms.CheckedListBox
        Me.lblColumns = New System.Windows.Forms.Label
        Me.chkPrimaryKey = New System.Windows.Forms.CheckBox
        Me.ErrorProvider1 = New System.Windows.Forms.ErrorProvider
        Me.SuspendLayout()
        '
        'lblConstraintName
        '
        Me.lblConstraintName.AccessibleDescription = resources.GetString("lblConstraintName.AccessibleDescription")
        Me.lblConstraintName.AccessibleName = resources.GetString("lblConstraintName.AccessibleName")
        Me.lblConstraintName.Anchor = CType(resources.GetObject("lblConstraintName.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.lblConstraintName.AutoSize = CType(resources.GetObject("lblConstraintName.AutoSize"), Boolean)
        Me.lblConstraintName.Dock = CType(resources.GetObject("lblConstraintName.Dock"), System.Windows.Forms.DockStyle)
        Me.lblConstraintName.Enabled = CType(resources.GetObject("lblConstraintName.Enabled"), Boolean)
        Me.ErrorProvider1.SetError(Me.lblConstraintName, resources.GetString("lblConstraintName.Error"))
        Me.lblConstraintName.Font = CType(resources.GetObject("lblConstraintName.Font"), System.Drawing.Font)
        Me.ErrorProvider1.SetIconAlignment(Me.lblConstraintName, CType(resources.GetObject("lblConstraintName.IconAlignment"), System.Windows.Forms.ErrorIconAlignment))
        Me.ErrorProvider1.SetIconPadding(Me.lblConstraintName, CType(resources.GetObject("lblConstraintName.IconPadding"), Integer))
        Me.lblConstraintName.Image = CType(resources.GetObject("lblConstraintName.Image"), System.Drawing.Image)
        Me.lblConstraintName.ImageAlign = CType(resources.GetObject("lblConstraintName.ImageAlign"), System.Drawing.ContentAlignment)
        Me.lblConstraintName.ImageIndex = CType(resources.GetObject("lblConstraintName.ImageIndex"), Integer)
        Me.lblConstraintName.ImeMode = CType(resources.GetObject("lblConstraintName.ImeMode"), System.Windows.Forms.ImeMode)
        Me.lblConstraintName.Location = CType(resources.GetObject("lblConstraintName.Location"), System.Drawing.Point)
        Me.lblConstraintName.Name = "lblConstraintName"
        Me.lblConstraintName.RightToLeft = CType(resources.GetObject("lblConstraintName.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.lblConstraintName.Size = CType(resources.GetObject("lblConstraintName.Size"), System.Drawing.Size)
        Me.lblConstraintName.TabIndex = CType(resources.GetObject("lblConstraintName.TabIndex"), Integer)
        Me.lblConstraintName.Text = resources.GetString("lblConstraintName.Text")
        Me.lblConstraintName.TextAlign = CType(resources.GetObject("lblConstraintName.TextAlign"), System.Drawing.ContentAlignment)
        Me.lblConstraintName.Visible = CType(resources.GetObject("lblConstraintName.Visible"), Boolean)
        '
        'txtConstraintName
        '
        Me.txtConstraintName.AccessibleDescription = resources.GetString("txtConstraintName.AccessibleDescription")
        Me.txtConstraintName.AccessibleName = resources.GetString("txtConstraintName.AccessibleName")
        Me.txtConstraintName.Anchor = CType(resources.GetObject("txtConstraintName.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.txtConstraintName.AutoSize = CType(resources.GetObject("txtConstraintName.AutoSize"), Boolean)
        Me.txtConstraintName.BackgroundImage = CType(resources.GetObject("txtConstraintName.BackgroundImage"), System.Drawing.Image)
        Me.txtConstraintName.Dock = CType(resources.GetObject("txtConstraintName.Dock"), System.Windows.Forms.DockStyle)
        Me.txtConstraintName.Enabled = CType(resources.GetObject("txtConstraintName.Enabled"), Boolean)
        Me.ErrorProvider1.SetError(Me.txtConstraintName, resources.GetString("txtConstraintName.Error"))
        Me.txtConstraintName.Font = CType(resources.GetObject("txtConstraintName.Font"), System.Drawing.Font)
        Me.ErrorProvider1.SetIconAlignment(Me.txtConstraintName, CType(resources.GetObject("txtConstraintName.IconAlignment"), System.Windows.Forms.ErrorIconAlignment))
        Me.ErrorProvider1.SetIconPadding(Me.txtConstraintName, CType(resources.GetObject("txtConstraintName.IconPadding"), Integer))
        Me.txtConstraintName.ImeMode = CType(resources.GetObject("txtConstraintName.ImeMode"), System.Windows.Forms.ImeMode)
        Me.txtConstraintName.Location = CType(resources.GetObject("txtConstraintName.Location"), System.Drawing.Point)
        Me.txtConstraintName.MaxLength = CType(resources.GetObject("txtConstraintName.MaxLength"), Integer)
        Me.txtConstraintName.Multiline = CType(resources.GetObject("txtConstraintName.Multiline"), Boolean)
        Me.txtConstraintName.Name = "txtConstraintName"
        Me.txtConstraintName.PasswordChar = CType(resources.GetObject("txtConstraintName.PasswordChar"), Char)
        Me.txtConstraintName.RightToLeft = CType(resources.GetObject("txtConstraintName.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.txtConstraintName.ScrollBars = CType(resources.GetObject("txtConstraintName.ScrollBars"), System.Windows.Forms.ScrollBars)
        Me.txtConstraintName.Size = CType(resources.GetObject("txtConstraintName.Size"), System.Drawing.Size)
        Me.txtConstraintName.TabIndex = CType(resources.GetObject("txtConstraintName.TabIndex"), Integer)
        Me.txtConstraintName.Text = resources.GetString("txtConstraintName.Text")
        Me.txtConstraintName.TextAlign = CType(resources.GetObject("txtConstraintName.TextAlign"), System.Windows.Forms.HorizontalAlignment)
        Me.txtConstraintName.Visible = CType(resources.GetObject("txtConstraintName.Visible"), Boolean)
        Me.txtConstraintName.WordWrap = CType(resources.GetObject("txtConstraintName.WordWrap"), Boolean)
        '
        'lstColumns
        '
        Me.lstColumns.AccessibleDescription = resources.GetString("lstColumns.AccessibleDescription")
        Me.lstColumns.AccessibleName = resources.GetString("lstColumns.AccessibleName")
        Me.lstColumns.Anchor = CType(resources.GetObject("lstColumns.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.lstColumns.BackgroundImage = CType(resources.GetObject("lstColumns.BackgroundImage"), System.Drawing.Image)
        Me.lstColumns.ColumnWidth = CType(resources.GetObject("lstColumns.ColumnWidth"), Integer)
        Me.lstColumns.Dock = CType(resources.GetObject("lstColumns.Dock"), System.Windows.Forms.DockStyle)
        Me.lstColumns.Enabled = CType(resources.GetObject("lstColumns.Enabled"), Boolean)
        Me.ErrorProvider1.SetError(Me.lstColumns, resources.GetString("lstColumns.Error"))
        Me.lstColumns.Font = CType(resources.GetObject("lstColumns.Font"), System.Drawing.Font)
        Me.lstColumns.HorizontalExtent = CType(resources.GetObject("lstColumns.HorizontalExtent"), Integer)
        Me.lstColumns.HorizontalScrollbar = CType(resources.GetObject("lstColumns.HorizontalScrollbar"), Boolean)
        Me.ErrorProvider1.SetIconAlignment(Me.lstColumns, CType(resources.GetObject("lstColumns.IconAlignment"), System.Windows.Forms.ErrorIconAlignment))
        Me.ErrorProvider1.SetIconPadding(Me.lstColumns, CType(resources.GetObject("lstColumns.IconPadding"), Integer))
        Me.lstColumns.ImeMode = CType(resources.GetObject("lstColumns.ImeMode"), System.Windows.Forms.ImeMode)
        Me.lstColumns.IntegralHeight = CType(resources.GetObject("lstColumns.IntegralHeight"), Boolean)
        Me.lstColumns.Location = CType(resources.GetObject("lstColumns.Location"), System.Drawing.Point)
        Me.lstColumns.Name = "lstColumns"
        Me.lstColumns.RightToLeft = CType(resources.GetObject("lstColumns.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.lstColumns.ScrollAlwaysVisible = CType(resources.GetObject("lstColumns.ScrollAlwaysVisible"), Boolean)
        Me.lstColumns.Size = CType(resources.GetObject("lstColumns.Size"), System.Drawing.Size)
        Me.lstColumns.TabIndex = CType(resources.GetObject("lstColumns.TabIndex"), Integer)
        Me.lstColumns.Visible = CType(resources.GetObject("lstColumns.Visible"), Boolean)
        '
        'lblColumns
        '
        Me.lblColumns.AccessibleDescription = resources.GetString("lblColumns.AccessibleDescription")
        Me.lblColumns.AccessibleName = resources.GetString("lblColumns.AccessibleName")
        Me.lblColumns.Anchor = CType(resources.GetObject("lblColumns.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.lblColumns.AutoSize = CType(resources.GetObject("lblColumns.AutoSize"), Boolean)
        Me.lblColumns.Dock = CType(resources.GetObject("lblColumns.Dock"), System.Windows.Forms.DockStyle)
        Me.lblColumns.Enabled = CType(resources.GetObject("lblColumns.Enabled"), Boolean)
        Me.ErrorProvider1.SetError(Me.lblColumns, resources.GetString("lblColumns.Error"))
        Me.lblColumns.Font = CType(resources.GetObject("lblColumns.Font"), System.Drawing.Font)
        Me.ErrorProvider1.SetIconAlignment(Me.lblColumns, CType(resources.GetObject("lblColumns.IconAlignment"), System.Windows.Forms.ErrorIconAlignment))
        Me.ErrorProvider1.SetIconPadding(Me.lblColumns, CType(resources.GetObject("lblColumns.IconPadding"), Integer))
        Me.lblColumns.Image = CType(resources.GetObject("lblColumns.Image"), System.Drawing.Image)
        Me.lblColumns.ImageAlign = CType(resources.GetObject("lblColumns.ImageAlign"), System.Drawing.ContentAlignment)
        Me.lblColumns.ImageIndex = CType(resources.GetObject("lblColumns.ImageIndex"), Integer)
        Me.lblColumns.ImeMode = CType(resources.GetObject("lblColumns.ImeMode"), System.Windows.Forms.ImeMode)
        Me.lblColumns.Location = CType(resources.GetObject("lblColumns.Location"), System.Drawing.Point)
        Me.lblColumns.Name = "lblColumns"
        Me.lblColumns.RightToLeft = CType(resources.GetObject("lblColumns.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.lblColumns.Size = CType(resources.GetObject("lblColumns.Size"), System.Drawing.Size)
        Me.lblColumns.TabIndex = CType(resources.GetObject("lblColumns.TabIndex"), Integer)
        Me.lblColumns.Text = resources.GetString("lblColumns.Text")
        Me.lblColumns.TextAlign = CType(resources.GetObject("lblColumns.TextAlign"), System.Drawing.ContentAlignment)
        Me.lblColumns.Visible = CType(resources.GetObject("lblColumns.Visible"), Boolean)
        '
        'chkPrimaryKey
        '
        Me.chkPrimaryKey.AccessibleDescription = resources.GetString("chkPrimaryKey.AccessibleDescription")
        Me.chkPrimaryKey.AccessibleName = resources.GetString("chkPrimaryKey.AccessibleName")
        Me.chkPrimaryKey.Anchor = CType(resources.GetObject("chkPrimaryKey.Anchor"), System.Windows.Forms.AnchorStyles)
        Me.chkPrimaryKey.Appearance = CType(resources.GetObject("chkPrimaryKey.Appearance"), System.Windows.Forms.Appearance)
        Me.chkPrimaryKey.BackgroundImage = CType(resources.GetObject("chkPrimaryKey.BackgroundImage"), System.Drawing.Image)
        Me.chkPrimaryKey.CheckAlign = CType(resources.GetObject("chkPrimaryKey.CheckAlign"), System.Drawing.ContentAlignment)
        Me.chkPrimaryKey.Dock = CType(resources.GetObject("chkPrimaryKey.Dock"), System.Windows.Forms.DockStyle)
        Me.chkPrimaryKey.Enabled = CType(resources.GetObject("chkPrimaryKey.Enabled"), Boolean)
        Me.ErrorProvider1.SetError(Me.chkPrimaryKey, resources.GetString("chkPrimaryKey.Error"))
        Me.chkPrimaryKey.FlatStyle = CType(resources.GetObject("chkPrimaryKey.FlatStyle"), System.Windows.Forms.FlatStyle)
        Me.chkPrimaryKey.Font = CType(resources.GetObject("chkPrimaryKey.Font"), System.Drawing.Font)
        Me.ErrorProvider1.SetIconAlignment(Me.chkPrimaryKey, CType(resources.GetObject("chkPrimaryKey.IconAlignment"), System.Windows.Forms.ErrorIconAlignment))
        Me.ErrorProvider1.SetIconPadding(Me.chkPrimaryKey, CType(resources.GetObject("chkPrimaryKey.IconPadding"), Integer))
        Me.chkPrimaryKey.Image = CType(resources.GetObject("chkPrimaryKey.Image"), System.Drawing.Image)
        Me.chkPrimaryKey.ImageAlign = CType(resources.GetObject("chkPrimaryKey.ImageAlign"), System.Drawing.ContentAlignment)
        Me.chkPrimaryKey.ImageIndex = CType(resources.GetObject("chkPrimaryKey.ImageIndex"), Integer)
        Me.chkPrimaryKey.ImeMode = CType(resources.GetObject("chkPrimaryKey.ImeMode"), System.Windows.Forms.ImeMode)
        Me.chkPrimaryKey.Location = CType(resources.GetObject("chkPrimaryKey.Location"), System.Drawing.Point)
        Me.chkPrimaryKey.Name = "chkPrimaryKey"
        Me.chkPrimaryKey.RightToLeft = CType(resources.GetObject("chkPrimaryKey.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.chkPrimaryKey.Size = CType(resources.GetObject("chkPrimaryKey.Size"), System.Drawing.Size)
        Me.chkPrimaryKey.TabIndex = CType(resources.GetObject("chkPrimaryKey.TabIndex"), Integer)
        Me.chkPrimaryKey.Text = resources.GetString("chkPrimaryKey.Text")
        Me.chkPrimaryKey.TextAlign = CType(resources.GetObject("chkPrimaryKey.TextAlign"), System.Drawing.ContentAlignment)
        Me.chkPrimaryKey.Visible = CType(resources.GetObject("chkPrimaryKey.Visible"), Boolean)
        '
        'ErrorProvider1
        '
        Me.ErrorProvider1.ContainerControl = Me
        Me.ErrorProvider1.Icon = CType(resources.GetObject("ErrorProvider1.Icon"), System.Drawing.Icon)
        '
        'UUniqueConstraint
        '
        Me.AccessibleDescription = resources.GetString("$this.AccessibleDescription")
        Me.AccessibleName = resources.GetString("$this.AccessibleName")
        Me.AutoScroll = CType(resources.GetObject("$this.AutoScroll"), Boolean)
        Me.AutoScrollMargin = CType(resources.GetObject("$this.AutoScrollMargin"), System.Drawing.Size)
        Me.AutoScrollMinSize = CType(resources.GetObject("$this.AutoScrollMinSize"), System.Drawing.Size)
        Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"), System.Drawing.Image)
        Me.Controls.Add(Me.lblConstraintName)
        Me.Controls.Add(Me.txtConstraintName)
        Me.Controls.Add(Me.lblColumns)
        Me.Controls.Add(Me.chkPrimaryKey)
        Me.Controls.Add(Me.lstColumns)
        Me.Enabled = CType(resources.GetObject("$this.Enabled"), Boolean)
        Me.ErrorProvider1.SetError(Me, resources.GetString("$this.Error"))
        Me.Font = CType(resources.GetObject("$this.Font"), System.Drawing.Font)
        Me.ErrorProvider1.SetIconAlignment(Me, CType(resources.GetObject("$this.IconAlignment"), System.Windows.Forms.ErrorIconAlignment))
        Me.ErrorProvider1.SetIconPadding(Me, CType(resources.GetObject("$this.IconPadding"), Integer))
        Me.ImeMode = CType(resources.GetObject("$this.ImeMode"), System.Windows.Forms.ImeMode)
        Me.Location = CType(resources.GetObject("$this.Location"), System.Drawing.Point)
        Me.Name = "UUniqueConstraint"
        Me.RightToLeft = CType(resources.GetObject("$this.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.Size = CType(resources.GetObject("$this.Size"), System.Drawing.Size)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private mOk As Boolean

    Public Event ValueChanged As EventHandler

    Private Sub URelation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub txtConstraintName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtConstraintName.TextChanged
        IsOk()
        OnValueChanged(Me, New EventArgs)
    End Sub
    Private Sub txtConstraintName_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtConstraintName.Validated
        If txtConstraintName.Text = "" Then
            ErrorProvider1.SetError(txtConstraintName, "Constraint Name is required.")
        Else
            ErrorProvider1.SetError(txtConstraintName, "")
        End If
    End Sub
    Private Sub lstColumns_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lstColumns.ItemCheck
        mOk = (txtConstraintName.Text <> "")
        If mOk Then
            If e.NewValue = CheckState.Unchecked And lstColumns.CheckedItems.Count = 1 Then mOk = False
        End If
        OnValueChanged(Me, New EventArgs)
    End Sub
    Private Sub chkPrimaryKey_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkPrimaryKey.CheckedChanged
        IsOk()
        OnValueChanged(Me, New EventArgs)
    End Sub

    Private Sub IsOk()
        mOk = (txtConstraintName.Text <> "")
        If mOk Then
            If lstColumns.CheckedItems.Count = 0 Then
                mOk = False
            End If
        End If
    End Sub

    Protected Sub OnValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        RaiseEvent ValueChanged(Me, New EventArgs)
    End Sub

    Public Function GetNewConstraint() As UniqueConstraint
        Dim pk() As DataColumn

        Dim dc As DataColumn, i As Integer
        For Each dc In lstColumns.CheckedItems
            ReDim Preserve pk(i) : pk(i) = dc
            i += 1
        Next : dc = Nothing

        GetNewConstraint = New UniqueConstraint(txtConstraintName.Text, pk, chkPrimaryKey.Checked)
        pk = Nothing
    End Function

    Public Property Ok() As Boolean
        Get
            Return mOk
        End Get
        Set(ByVal Value As Boolean)
            mOk = False
        End Set
    End Property

    Public Sub Prepare(ByVal table As Model10.Table)
        lstColumns.Items.Clear()

        Dim dc As DataColumn
        For Each dc In table.DataTable.Columns
            lstColumns.Items.Add(dc)
        Next : dc = Nothing
    End Sub

    Public Sub Read(ByVal uniqueConstraint As UniqueConstraint)
        txtConstraintName.Text = uniqueConstraint.ConstraintName
        '-------------------
        Dim dc As DataColumn
        For Each dc In uniqueConstraint.Columns
            lstColumns.SetItemChecked(lstColumns.Items.IndexOf(dc), True)
        Next : dc = Nothing
        '-------------------
        chkPrimaryKey.Checked = uniqueConstraint.IsPrimaryKey
    End Sub
    Public Sub Write(ByVal UniqueConstraint As UniqueConstraint)
        UniqueConstraint.ConstraintName = txtConstraintName.Text
    End Sub
End Class